package com.cxh.rmsq.ui.adapter.self;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cxh.rmsq.databinding.AdapterCampClockImagesBinding;
import com.cxh.rmsq.databinding.AdapterCampClockImagesNewBinding;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

public class CampClockImageNewAdapter extends RecyclerView.Adapter<CampClockImageNewAdapter.CampClockImageViewHolder> {

    private final Context context;
    private final List<LocalMedia> imagesMediaList;

    public CampClockImageNewAdapter(Context context, List<LocalMedia> imagesMediaList) {
        this.context = context;
        this.imagesMediaList = imagesMediaList;
    }

    @NonNull
    @Override
    public CampClockImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new CampClockImageViewHolder(AdapterCampClockImagesNewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CampClockImageViewHolder holder, int position) {
        if (position == imagesMediaList.size()) {
            holder.binding.ivCampClockImage.setVisibility(View.GONE);
            holder.binding.ivCampClockImageAdd.setVisibility(View.VISIBLE);

            holder.binding.layoutCampClockImageAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mItemClickListener != null) {
                        mItemClickListener.openPicture();
                    }
                }
            });
        } else {
            holder.binding.ivCampClockImage.setVisibility(View.VISIBLE);
            holder.binding.ivCampClockImageAdd.setVisibility(View.GONE);

            LocalMedia media = imagesMediaList.get(position);
            String path = media.getAvailablePath();
            Glide.with(context)
                    .load(PictureMimeType.isContent(path) && !media.isCut() && !media.isCompressed() ? Uri.parse(path) : path)
                    .into(holder.binding.ivCampClockImage);

            holder.binding.layoutCampClockImageAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(view, holder.getAbsoluteAdapterPosition());
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return imagesMediaList.size() + 1;
    }

    private OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

        void openPicture();
    }

    static class CampClockImageViewHolder extends RecyclerView.ViewHolder {

        AdapterCampClockImagesNewBinding binding;

        public CampClockImageViewHolder(AdapterCampClockImagesNewBinding binding) {
            super(binding.getRoot());

            this.binding = binding;
        }
    }
}
